package com.crm.util;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Map;

import org.apache.log4j.Logger;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonUtil {
	
	private static Logger logger = Logger.getLogger(JsonUtil.class);
	
	private JSONObject o;

	public JsonUtil(JSONObject o) {
		this.o = o;
	}

	public String getString(String key) {
		return this.o.getString(key).equals("null") ? null : this.o.getString(key);
	}

	public BigDecimal getBigDecimal(String key) {
		String val = getString(key);
		return val == null ? new BigDecimal(0) : new BigDecimal(val);
	}

	public Timestamp getTimestamp(String key) {
		String val = getString(key);
		if ((val == null) || ("".equals(val))) {
			return null;
		}
		try {
			return SqlDateUtil.parseTimestamp(val);
		} catch (Exception e) {
			logger.error(e);
		}
		return new Timestamp(new java.util.Date(val).getTime());
	}

	public static String beanToJson(Object object) {
		String jsonString = null;

		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(java.sql.Date.class,
				new JsonDateValueProcessor());
		jsonConfig.registerJsonValueProcessor(Timestamp.class,
				new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
		if (object != null) {
			if (((object instanceof Collection))
					|| ((object instanceof Object[])))
				jsonString = JSONArray.fromObject(object, jsonConfig)
						.toString();
			else {
				jsonString = JSONObject.fromObject(object, jsonConfig)
						.toString();
			}
		}
		return jsonString == null ? "{}" : jsonString;
	}

	public static String beanToJson(Object object, JsonConfig jsonConfig) {
		String jsonString = null;
		if (object != null) {
			if (((object instanceof Collection))
					|| ((object instanceof Object[])))
				jsonString = JSONArray.fromObject(object, jsonConfig)
						.toString();
			else {
				jsonString = JSONObject.fromObject(object, jsonConfig)
						.toString();
			}
		}
		return jsonString == null ? "{}" : jsonString;
	}

	public static Object jsonToBean(JSONObject jsonObject, Class beanClass) {
		return JSONObject.toBean(jsonObject, beanClass);
	}

	public static Object jsonToBean(JSONObject jsonObject, Class beanClass,
			Map map) {
		Object object = JSONObject.toBean(jsonObject, beanClass, map);
		return object;
	}

	public static class JsonDateValueProcessor implements JsonValueProcessor {
		private String dateFormat = "yyyy-MM-dd";

		public JsonDateValueProcessor() {
		}

		public JsonDateValueProcessor(String dateFormat) {
			this.dateFormat = dateFormat;
		}

		public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			String[] obj = new String[0];
			if ((value instanceof java.sql.Date[])) {
				SimpleDateFormat sf = new SimpleDateFormat(this.dateFormat);
				java.sql.Date[] dates = (java.sql.Date[]) value;
				obj = new String[dates.length];
				for (int i = 0; i < dates.length; i++) {
					obj[i] = sf.format(dates[i]);
				}
			}
			return obj;
		}

		public Object processObjectValue(String key, Object value,
				JsonConfig jsonConfig) {
			String dateStr = "";

			if ((value instanceof java.sql.Date)) {
				dateStr = new SimpleDateFormat(this.dateFormat)
						.format((java.sql.Date) value);
			}

			if (dateStr.length() > 0) {
				return dateStr;
			}
			return value == null ? null : value.toString();
		}
	}
}